<template>
  <el-dialog
    title="编辑"
    v-model="dialogVisible"
    @close="cancelFunc"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    width="30%"
  >
    <el-form :model="formData">
      <el-form-item label="微信昵称" :label-width="formLabelWidth"
        ><el-input
          type="text"
          v-model="formData.nickName"
          autocomplete="off"
          :disabled="true"
        ></el-input
      ></el-form-item>
      <el-form-item label="微信头像" :label-width="formLabelWidth"
        ><img v-img-url="formData.avatarUrl" width="50" height="50"
      /></el-form-item>
      <el-form-item label="姓名" :label-width="formLabelWidth"
        ><el-input
          type="text"
          v-model="formData.realName"
          autocomplete="off"
        ></el-input
      ></el-form-item>
      <el-form-item label="手机号" :label-width="formLabelWidth"
        ><el-input
          type="text"
          v-model="formData.mobile"
          autocomplete="off"
        ></el-input
      ></el-form-item>
      <el-form-item label="推荐人ID" :label-width="formLabelWidth">
        <el-input
          type="text"
          v-model="formData.refereeId"
          autocomplete="off"
        ></el-input>
        <div class="tips">如果没有上级则设置为0</div>
      </el-form-item>
      <el-form-item label="推荐人昵称" :label-width="formLabelWidth">
        <span v-if="formData.referee">{{ formData.referee.nickName }}</span>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancelFunc">取 消</el-button>
        <el-button type="primary" @click="confirmFunc">确 定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import PlusApi from "@/api/plus.js";
export default {
  data() {
    return {
      /*左边长度*/
      formLabelWidth: "100px",
      /*是否显示*/
      dialogVisible: false,
      /*表单数据模型*/
      formData: {},
      old_referee_id: 0,
    };
  },
  props: {
    open_edit: Boolean,
    userModel: Object,
  },
  watch: {
    open_edit: function (n, o) {
      if (n != o) {
        this.dialogVisible = this.open_edit;
        if (n) {
          this.formData = this.userModel;
          this.old_referee_id = this.formData.refereeId;
        }
      }
    },
  },
  created() {},
  methods: {
    /*修改用户*/
    confirmFunc() {
      let self = this;
      let params = {};
      params.userId = this.formData.userId;
      params.realName = this.formData.realName;
      params.mobile = this.formData.mobile;
      if (
        self.formData.refereeId > 0 &&
        self.formData.refereeId != self.old_referee_id
      ) {
        PlusApi.toAgentUserEdit(
          {
            userId: self.formData.refereeId,
          },
          true
        )
          .then((res) => {
            if (!res.data) {
              ElMessage({
                message: "上级用户不存在",
                type: "error",
              });
              return;
            }
            if (this.formData.userId == self.formData.refereeId) {
              ElMessage({
                message: "不能绑定自己",
                type: "error",
              });
              return;
            }
            ElMessageBox.confirm(
              "确定要修改上级到" + res.data.realName + "?",
              "提示",
              {
                type: "warning",
              }
            ).then(() => {
              params.refereeId = self.formData.refereeId;
              self.editUser(params);
            });
          })
          .catch((error) => {});
      } else {
        params.refereeId = 0;
        self.editUser(params);
      }
    },
    editUser(params) {
      let self = this;
      console.log(params, "params");
      PlusApi.agentUserEdit(params, true)
        .then((data) => {
          ElMessage({
            message: "恭喜你，修改成功",
            type: "success",
          });
          self.cancelFunc(true);
        })
        .catch((error) => {});
    },
    /*关闭弹窗*/
    cancelFunc(e) {
      let type = "cancel";
      if (e) {
        type = "success";
      }
      this.$emit("close", {
        type: type,
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>
